package com.qf.sys.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.sys.po.Dept;
import com.qf.sys.po.Role;
import com.qf.sys.po.User;
import com.qf.sys.service.RoleService;
import com.qf.sys.service.UserService;
import com.qf.sys.util.LayUIOperate;
import com.qf.sys.util.Msg;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @创建人 sunsir Email:sunsir163@163.com
 * @创建时间 2022/5/6
 * @描述
 */
@Controller
@RequestMapping("user")
public class UserController {
    @Resource
    UserService userService;
    @Resource
    RoleService roleService;
    @RequestMapping("getUserList")
    @ResponseBody
    public Msg getUserList(HttpServletRequest request){
        int pageSize =Integer.parseInt(request.getParameter("limit"));
        int pageNumber = Integer.parseInt(request.getParameter("page"));
        String _empNo = request.getParameter("empNo");
        int empNo = 0;
        if (_empNo != null && !"".equals(_empNo)) {
            empNo = Integer.parseInt(_empNo);
        }
        String _deptNo = request.getParameter("deptNo");
        int deptNo = 0;
        if (_deptNo != null && !"".equals(_deptNo)) {
            deptNo = Integer.parseInt(_deptNo);
        }
        String ename = request.getParameter("ename");
        String _status = request.getParameter("status");
        System.out.println(_status);
        int status = 3;
        if (_status != null && !"".equals(_status)) {
            status = Integer.parseInt(_status);
        }
        Map params = new HashMap();
        params.put("empNo", empNo);
        params.put("ename", ename);
        params.put("deptNo", deptNo);
        params.put("status", status);
        System.out.println(status);
        PageHelper.startPage(pageNumber, pageSize);
        PageInfo<User> data = userService.getUserList(params);
        Msg msg = new Msg();
        msg.setCode(0);
        msg.setMsg("处理成功");
        msg.setCount(data.getTotal());//得到总条数
        msg.setData(data.getList());//得到查出的List数据
        System.out.println(msg);
        return msg;
    }
    @RequestMapping("addUser")
    @ResponseBody
    public LayUIOperate addUser(@RequestBody Map<String,Object> params){
        LayUIOperate operate=new LayUIOperate();
        System.out.println("addUser -->"+params);

        /*
        User mgr = userService.getUserById(empNo);
        Role role = roleService.getRoleById(roleid);*/
        boolean f= userService.addUser(params);
        if(f){
            operate.setSuccess(true);
            operate.setMessage("用户添加成功");
        }else{
            operate.setSuccess(false);
            operate.setMessage("用户添加失败");
        }
        return operate;
    }
    @RequestMapping("updateUser")
    @ResponseBody
    public LayUIOperate updateUser(@RequestBody User user){
        LayUIOperate operate=new LayUIOperate();
        System.out.println("updateUser -->"+user);
        boolean f= userService.updateUser(user);
        if(f){
            operate.setSuccess(true);
            operate.setMessage("用户修改成功！牛逼");
        }else{
            operate.setSuccess(false);
            operate.setMessage("用户修改失败");
        }
        return operate;
    }

    @RequestMapping("deleteUser")
    @ResponseBody
    public LayUIOperate deleteUser(Integer id){
        LayUIOperate operate=new LayUIOperate();
        boolean f= userService.deleteUser(id);
        if(f){
            operate.setSuccess(true);
            operate.setMessage("用户删除成功！牛逼");
        }else{
            operate.setSuccess(false);
            operate.setMessage("用户删除失败");
        }
        return operate;
    }
    @RequestMapping("upadteUserstatusTo0")
    @ResponseBody
    public LayUIOperate upadteUserstatusTo0(int empNo){
        LayUIOperate operate=new LayUIOperate();
        boolean f= userService.upadteUserstatusTo0(empNo);
        if(f){
            operate.setSuccess(true);
            operate.setMessage("用户删除成功！牛逼");
        }else{
            operate.setSuccess(false);
            operate.setMessage("用户删除失败");
        }
        return operate;
    }
    @RequestMapping("upadteUserstatusTo1")
    @ResponseBody
    public LayUIOperate upadteUserstatusTo1(int empNo){
        LayUIOperate operate=new LayUIOperate();
        boolean f= userService.upadteUserstatusTo1(empNo);
        if(f){
            operate.setSuccess(true);
            operate.setMessage("用户删除成功！牛逼");
        }else{
            operate.setSuccess(false);
            operate.setMessage("用户删除失败");
        }
        return operate;
    }

}
